app.controller('AccountCtrl',
    function($scope, $state, $rootScope, $timeout, $localStorage, toaster, accountService){
        $scope.account = {
            username : 'admin',
            password : '123456',
            islogined : false
        };
        $rootScope.token
            = $localStorage.token;
        $scope.onLogin = function(){

            var promise = accountService.login($scope.account);

            promise.then(function(res){
                //登陆成功

                  $localStorage.token
                    = res.access_token;
                $localStorage.userInfo=res;
                $localStorage.roleid=res.roleid;
                $rootScope.roleid=$localStorage.roleid;//角色单位
                // $scope.successAlert(' 欢迎 ' + res.name + ' !');
                $state.go('app.home');
                /*toaster.pop("success", "提示", "欢迎"+res.name);
                $timeout(function(){
                    $state.go('app.home');
                }, 500);*/
            }, function(err){

                // $scope.errorAlert(err.error_description);
                toaster.error("错误", err.error_description);
                // toaster.error("错误", err.error_description);

            });
            /*$timeout(function(){
                $scope.alerts.length = 0;
            }, 3000);*/

        };

        $scope.onLogout = function(){
            accountService.login($scope.account);
        };
        /**
         * 清空token
         */
        $scope.clearToken = function(){
            console.log(" btn clear clicked! ");
            $localStorage.token = "";
        }
        //调用alert
        $scope.alerts = [];

        $scope.successAlert = function(msg){
            $scope.alerts.push({type : 'success', msg : msg});
        };
        $scope.errorAlert = function(msg){
            $scope.alerts.push({type : 'danger', msg : msg});
        };
        $scope.closeAlert = function(index){
            $scope.alerts.splice(index, 1);
        };
        $scope.canvasInit=function(){
            $('.canvas').line();
        }
        // alert end
        $scope.clearToken();
        $scope.onLogin();
    });